package com.course.business.controller.admin;

import com.course.server.common.Result;
import com.course.server.dto.CourseContentDto;
import com.course.server.dto.CourseDto;
import com.course.server.query.CoursePageQuery;
import com.course.server.service.CourseCategoryService;
import com.course.server.service.CourseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * @author: Durian
 * @date: 2021/5/2 11:05
 */
@Validated
@RestController
@RequestMapping("/admin/courses")
public class CourseController {

    /**
     * 用于日志记录使用
     */
    public static final String BUSINESS_NAME = "课程";

    @Resource
    private CourseService courseService;
    @Resource
    private CourseCategoryService courseCategoryService;

    @GetMapping
    public Result list(@Validated CoursePageQuery request) {
        return Result.success(courseService.list(request.getPage(), request.getSize(), request.getCourseId()));
    }

    @GetMapping("/categories/{courseId}")
    public Result listCategories(@PathVariable("courseId") String courseId) {
        return Result.success(courseCategoryService.listBYCourseId(courseId));
    }

    @PostMapping
    public Result save(@RequestBody @Validated CourseDto courseDto) {
        courseService.save(courseDto);
        return Result.success();
    }

    @DeleteMapping("{courseId}")
    public Result delete(@PathVariable("courseId") @NotBlank(message = "待删除课程ID不能为空") String courseId) {
        courseService.delete(courseId);
        return Result.success();
    }

    @GetMapping("/contents/{id}")
    public Result findCourseContents(@PathVariable("id") String id) {
        return Result.success(courseService.findCourseContent(id));
    }

    @PostMapping("/contents")
    public Result saveCourseContent(@RequestBody CourseContentDto courseContentDto) {
        courseService.saveCourseContent(courseContentDto);
        return Result.success();
    }
}
